from urllib.parse import urljoin
from common.send_requests import sendRequest
from Model.environment import Environment


class User:
    def __init__(self, username, password, env: Environment):
        self.username = username
        self.password = password
        self.env = env


class PcUser(User):

    headers = {
        "Content-Type": "application/json; charset=utf-8"
    }

    def login(self):
        # PC端登录
        url = urljoin(self.env.realmNameToken, "connect/token")
        headers = {
            "Content-Type": "application/x-www-form-urlencoded"
        }
        data = f"username={self.username}&password={self.password}&grant_type=password&client_id=backend-admin-app-client&client_secret=1q2w3e*"
        res = sendRequest(url, "post", headers=headers,
                          msg="token", data=data, oper="login")
        if str(res.status_code)[0] == "2":
            js_res = res.json()
        else:
            return ""
        if js_res.get("access_token", None):
            self.token = res.json()["access_token"]
        else:
            self.token = ""
        self.headers["Authorization"] = f"Bearer {self.token}"
        print(f'token: {self.headers["Authorization"]}')


class AppUser(User):
    headers = {
        "Content-Type": "application/json; charset=utf-8"
    }
    driver_phone = None
    driver_name = None
    driver_id_card = None

    def login(self):
        # PC端登录
        url = urljoin(self.env.realmNameToken, "connect/token")
        headers = {
            "Content-Type": "application/x-www-form-urlencoded"
        }
        data = f"grant_type=password&username={self.username}&password={self.password}&client_id=backend-admin-app-client&client_secret=1q2w3e*"
        res = sendRequest(url, "post", headers=headers,
                          msg="token", data=data, oper="login")
        if str(res.status_code)[0] == "2":
            js_res = res.json()
        else:
            return ""
        if js_res.get("access_token", None):
            self.token = res.json()["access_token"]
        else:
            self.token = ""
        self.headers["Authorization"] = f"Bearer {self.token}"
        print(f'token: {self.headers["Authorization"]}')

    def get_driver_info(self, reset=False):
        # 获取司机信息
        if reset:
            url = urljoin(self.env.realmName, "driver-client/driver")
            res = sendRequest(url, "get", headers=self.headers,
                              msg="获取司机信息")

            if res.status_code == 200:
                self.is_tag = True
                self.driver_phone = res.json()["mobile"]
                self.driver_name = res.json()["name"]
                self.driver_id_card = res.json()["idCard"]
                self.status = "获取司机信息成功"
            else:
                self.is_tag = False
                self.driver_phone = ""
                self.status = f"获取司机信息报错: {res.text}"
        else:
            if not self.driver_phone:
                self.get_driver_info(True)
